🎯 Top 50 Most Asked Tricky Java Output Problems
🟢 Beginner Level (1-15)
1️⃣ String Pool Gotcha
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
Output:
false
true
Explanation: == checks reference equality, .equals() checks value equality.
2️⃣ Integer Caching
public class Test {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println(a == b);
Integer c = 128;
Integer d = 128;
System.out.println(c == d);
}
}
Output:
true
false
Explanation: Java caches Integer objects from -128 to 127.
3️⃣ Pre/Post Increment
public class Test {
public static void main(String[] args) {
int i = 5;
System.out.println(i++ + ++i);
}
}
Output:
12
Explanation: i++ uses 5 then increments to 6, ++i increments to 7 then uses it → 5+7=12.
4️⃣ Static Block Execution Order
class A {
static { System.out.print("A"); }
}
class B extends A {
static { System.out.print("B"); }
}
public class Test {
public static void main(String[] args) {
B obj = new B();
}
}
Output:
AB
Explanation: Parent static block executes before child static block.
5️⃣ Overloaded Methods & Null
public class Test {
public static void main(String[] args) {
print(null);
}
static void print(Object o) { System.out.println("Object"); }
static void print(String s) { System.out.println("String"); }
}
Output:
String
Explanation: Null matches the most specific type (String is more specific than Object).
6️⃣ Polymorphism + Overloading
class A { }
class B extends A { }
public class Test {
static void show(A a) { System.out.println("A"); }
static void show(B b) { System.out.println("B"); }
public static void main(String[] args) {
A obj = new B();
show(obj);
}
}
Output:
A
Explanation: Overloaded method is chosen at compile-time based on reference type, not runtime type.
7️⃣ String Immutability
public class Test {
public static void main(String[] args) {
String s = "abc";
s.concat("def");
System.out.println(s);
}
}
Output:
abc
Explanation: String is immutable; concat() creates a new string, but result is not stored.
8️⃣ Arrays.asList() Trap
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3);
list.remove(2);
System.out.println(list);
}
}
Output:
UnsupportedOperationException
Explanation: Arrays.asList() returns a fixed-size list backed by an array.
9️⃣ Floating Point Precision
public class Test {
public static void main(String[] args) {
System.out.println(0.1 + 0.2 == 0.3);
}
}
Output:
false
Explanation: Binary floating-point representation cannot exactly represent 0.1 and 0.2.
🔟 Final Reference Mutation
import java.util.*;
public class Test {
public static void main(String[] args) {
final List<String> list = new ArrayList<>();
list.add("A");
System.out.println(list);
}
}
Output:
[A]
Explanation: final prevents reassignment of the reference, not mutation of the object.
1️⃣1️⃣ Constructor Chaining
class Parent {
Parent() { System.out.print("1"); }
}
class Child extends Parent {
Child() { System.out.print("2"); }
}
public class Test {
public static void main(String[] args) {
new Child();
}
}
Output:
12
Explanation: Parent constructor is called before child constructor.
1️⃣2️⃣ Division by Zero
public class Test {
public static void main(String[] args) {
System.out.println(10 / 0);
System.out.println(10.0 / 0);
}
}
Output:
ArithmeticException: / by zero
Explanation: Integer division by zero throws exception, but line 2 never executes.
1️⃣3️⃣ String Concatenation
public class Test {
public static void main(String[] args) {
System.out.println(1 + 2 + "3");
System.out.println("1" + 2 + 3);
}
}
Output:
33
123
Explanation: Left-to-right evaluation; numbers add first, then concatenate in line 1; all concatenate in line 2.
1️⃣4️⃣ Boolean Auto-boxing
public class Test {
public static void main(String[] args) {
Boolean b1 = true;
Boolean b2 = true;
Boolean b3 = new Boolean(true);
System.out.println(b1 == b2);
System.out.println(b1 == b3);
}
}
Output:
true
false
Explanation: Boolean caches true and false, but new Boolean() creates a new object.
1️⃣5️⃣ Switch Fall-through
public class Test {
public static void main(String[] args) {
int x = 1;
switch(x) {
case 1: System.out.print("A");
case 2: System.out.print("B");
default: System.out.print("C");
}
}
}
Output:
ABC
Explanation: No break statements cause fall-through to all subsequent cases.
🟡 Intermediate Level (16-35)
1️⃣6️⃣ Method Overriding with Covariant Return
class Parent {
Object test() { return "Parent"; }
}
class Child extends Parent {
String test() { return "Child"; }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.test());
}
}
Output:
Child
Explanation: Covariant return types allow child class to return a subtype. Runtime polymorphism applies.
1️⃣7️⃣ Static Method Hiding
class Parent {
static void test() { System.out.println("Parent"); }
}
class Child extends Parent {
static void test() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.test();
}
}
Output:
Parent
Explanation: Static methods are hidden, not overridden. Called based on reference type.
1️⃣8️⃣ Exception in Constructor
class A {
A() throws Exception {
throw new Exception("Error");
}
}
public class Test {
public static void main(String[] args) {
try {
new A();
} catch(Exception e) {
System.out.println("Caught");
}
}
}
Output:
Caught
Explanation: Constructor can throw checked exceptions if declared.
1️⃣9️⃣ Array Covariance
public class Test {
public static void main(String[] args) {
Object[] arr = new String[3];
arr[0] = "Hello";
arr[1] = 100;
}
}
Output:
ArrayStoreException
Explanation: Arrays are covariant, but runtime type checking prevents storing wrong type.
2️⃣0️⃣ Ternary Operator Type Promotion
public class Test {
public static void main(String[] args) {
char c = 'a';
int i = 97;
System.out.println(true ? c : i);
System.out.println(false ? c : i);
}
}
Output:
a
97
Explanation: Result type is promoted to common type (int), but char 'a' prints as 'a'.
2️⃣1️⃣ StringBuilder vs StringBuffer
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
}
}
Output:
Hello World
Explanation: StringBuilder is mutable and modifies the original object.
2️ ⃣2️⃣ Multiple Inheritance Diamond
interface A {
default void test() { System.out.println("A"); }
}
interface B {
default void test() { System.out.println("B"); }
}
class C implements A, B {
public void test() { System.out.println("C"); }
}
public class Test {
public static void main(String[] args) {
new C().test();
}
}
Output:
C
Explanation: Class method overrides default methods from interfaces.
2️⃣3️⃣ ArrayList Remove by Index vs Value
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
list.remove(2);
list.remove(Integer.valueOf(2));
System.out.println(list);
}
}
Output:
[1, 4]
Explanation: First removes index 2 (value 3), then removes value 2.
2️⃣4️⃣ NaN Comparison
public class Test {
public static void main(String[] args) {
double d = 0.0 / 0.0;
System.out.println(d == d);
System.out.println(Double.isNaN(d));
}
}
Output:
false
true
Explanation: NaN is not equal to itself; use isNaN() to check.
2️⃣5️⃣ Instance Initializer Block
class Test {
{
System.out.print("A");
}
Test() {
System.out.print("B");
}
{
System.out.print("C");
}
public static void main(String[] args) {
new Test();
}
}
Output:
ACB
Explanation: Instance initializer blocks run before constructor, in order of appearance.
2️⃣6️⃣ Short Circuit Evaluation
public class Test {
public static void main(String[] args) {
int x = 0;
if(false & (++x > 0)) { }
System.out.println(x);
int y = 0;
if(false && (++y > 0)) { }
System.out.println(y);
}
}
Output:
1
0
Explanation: & evaluates both sides, && short-circuits.
2️⃣7️⃣ Generic Type Erasure
import java.util.*;
public class Test {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
System.out.println(list1.getClass() == list2.getClass());
}
}
Output:
true
Explanation: Generic types are erased at runtime; both are ArrayList.
2️⃣8️⃣ Varargs Ambiguity
public class Test {
static void test(int... nums) { System.out.println("int"); }
static void test(Integer... nums) { System.out.println("Integer"); }
public static void main(String[] args) {
test(1, 2, 3);
}
}
Output:
Compilation Error: ambiguous method call
Explanation: Compiler cannot decide between primitive and wrapper varargs.
2️⃣9️⃣ Interface Variable Hiding
interface A {
int X = 10;
}
interface B {
int X = 20;
}
class C implements A, B {
public static void main(String[] args) {
System.out.println(X);
}
}
Output:
Compilation Error: reference to X is ambiguous
Explanation: Both interfaces define X; must use A.X or B.X to resolve.
3️⃣0️⃣ Finally Block Return
public class Test {
static int test() {
try {
return 1;
} finally {
return 2;
}
}
public static void main(String[] args) {
System.out.println(test());
}
}
Output:
2
Explanation: Finally block return overrides try block return.